// pages/project/object/object.js
const app = getApp()
Page({
  //  parents:{},
  /**
   * 页面的初始数据
   */
  data: {
    isShow: false,  // false为隐藏
    parents:{},
    name:'',
    arr:[ ],
    showView: false,
  },
    /*
    * isShow做取反操作
    * */

    // 点击快速定位，跳转页面
  goSmartSeek: function (e) {
    let id = e.currentTarget.dataset.id;
    var mainId;
    if(this.data.parents.parentId === '-1'){
         mainId = this.data.parents.subjectId;
         wx.navigateTo({
          url: '/pages/project/SmartSeek/SmartSeek?type=2&parentId='+ mainId + "&status=1", 
        })
    }else{
         mainId = this.data.parents.parentId; 
         wx.navigateTo({
          url: '/pages/project/SmartSeek/SmartSeek?type=2&parentId='+ mainId + "&status=2", 
        })
    } 
    
    this.setData({
      showView: false,
    })
  },
   // 点击快速巡检，跳转页面
   goQuickInspection: function (e) {
    let id = e.currentTarget.dataset.id;
    var mainId;
    var that = this;
    //选择图片
    wx.chooseImage({
      count: 1, // 默认9，这里显示一次选择相册的图片数量 
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) { // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        let tempFiles = res.tempFiles
        //把选择的图片 添加到集合里
        for (let i in tempFiles) {
          tempFiles[i]['upload_percent'] = 0
          tempFiles[i]['path_server'] = ''
          if (tempFiles[i]['upload_percent'] == 0) {          
          //上传返回值
          const upload_task = wx.uploadFile({
            // 模拟https
            url: app.BASE_SERVER_URL + '/api/wx/fileUpload', //需要用HTTPS，同时在微信公众平台后台添加服务器地址  
            filePath: tempFiles[i]['path'], //上传的文件本地地址    
            name: 'file',
            formData: {
              'num': 1
            },
            //附近数据，这里为路径     
            success: function(res) {
              console.log(res.data);
              var data = JSON.parse(res.data);
              // debugger
              if(that.data.parents.parentId === '-1'){
                mainId = that.data.parents.subjectId;
                wx.navigateTo({
                 url: '/pages/project/QuickInspection/QuickInspection?type=2&parentId='+ mainId + "&status=1&pic="+data.data.url, 
               })
           }else{
                mainId = that.data.parents.parentId; 
                wx.navigateTo({
                 url: '/pages/project/QuickInspection/QuickInspection?type=2&parentId='+ mainId + "&status=2&pic="+data.data.url, 
               })
           } 
           that.setData({
             showView: false,
           })
              // this.setData({
              //   showView: false,
              // })
            }
          }
        )}
      }       
    }
  }) 



  },

   // 点击数据，跳转页面
   goProdata: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/project/ProData/ProData?parentId='+id +"&type=2",
    })
  },
  // 点击图片，显示隐藏内容
  onShowTitle:function(e){
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
   toChange (e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let arr =  that.data.arr
    for(let i = 0 ; i < arr.length; i++) {
      if (index == i) {
        arr[i] = !arr[i]
      } else {
        arr[i] = false
      }
    }
    console.log(index)
    that.setData({
      arr:arr
    })
  },
    // 点击子对象
    goObject: function (e) {
      let id = e.currentTarget.dataset.id;
      let that = this; 
      let pam = that.data.parents;
      // let pam = that.onLoad(this.data.parents);
      console.log(pam)
      wx.navigateTo({
        url: '../object/object?subjectId= ' + pam.subjectId +'&parentId=' + id,
      })
    },
     // 点击指标按钮跳转到指标管理页面
    goTarget: function (e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../target/target?mainId=' + id +'&userId=' + this.data.userId,
      })
    },
    // 点击属性，跳转到属性页面
    goObjProper: function (e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '../object/ObjProper/ObjProper?mainId=' + id ,
      })
    },
    projectList:function(){
      let that = this

      wx.request({
        url: app.BASE_SERVER_URL + '/api/wx/getChildByObjId',
        method: 'POST',
        data:{
          parentId: this.data.parents.parentId,
          subjectId: this.data.parents.subjectId,
          name:this.data.name
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        success (res) { 
          let arr = res.data.data.list.map(() => false)
          that.setData({
            arr : arr,
            list:res.data.data.list   
          })
          console.log(res.data);     
        },
        fail:(err)=>{
          console.log(err)
        }
      })            
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    that.setData({
      parents:options,
    })
    this.projectList()
  },
  /**
   * 输入监听
   */
  searchInputAction: function (e) {
    this.setData({
      name:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userId:app.login_s.id
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})